<?php

//获取部门名称，递归
function department($id,$string = ''){
	if(getcache('HR_CONFIG::department_'.$id)!=''){
		return getcache('HR_CONFIG::department_'.$id);
	}
	$name = deep_department($id);
	setcache('HR_CONFIG::department_'.$id,$name);
	return $name;
}

//递归获取部门名称
function deep_department($id,$string=''){
	$info = D('hr_department')->where(['id'=>$id])->find();
	if(!$info){
		return '部门错误';
	}
	if($info['parentid']==0){
		if($string){
			$info['name'] .= '->'.$string;
		}
		return $info['name'];
	}else{
		return deep_department($info['parentid'],$info['name']);
	}
}

//获取职位名称
function position($id){
	if(!$id){
		return;
	}
	$position =  getcache('HR_CONFIG::position');
	if($position && isset($position[$id])){
		return $position[$id];
	}else{
		$position = D('hr_position')->select();
		$arr = [];
		foreach($position as $v){
			$arr[$v['id']] = $v;
		}
		setcache('HR_CONFIG::position',$arr);
		return $arr[$id];
	}
}


//计算员工年假
function calc_member_leave($member_info){
	if(!is_array($member_info)){
		$member_info = D('hr_member')->where(['id'=>$member_info])->find();
	}
	$position = position($member_info['position_id']);
	//可休假天数
	$days = $position['leave_days'];
	//过期时间
	$years = get_date_distance($member_info['in_time']);
	if($years<1){
		return array('status'=>0,'message'=>'工龄未满1年');
	}
	$start = strtotime('+'.($years).' years',$member_info['in_time'])-86400;
	$end = strtotime('+'.($years+1).' years',$member_info['in_time']);
	
	$total_days = D('hr_leave_list')->field('sum(days) days')->where('member_id = '.$member_info['id'].' AND action_time>'.$start.' AND action_time<'.$end)->one();
	
	$leave_days = $days-$total_days;
	$result = D('hr_member')->update(['leave_days'=>$leave_days,'leave_expire'=>$end],['id'=>$member_info['id']]);
	if($result!==false){
		return array('status'=>1,'message'=>'更新成功');
	}else{
		return array('status'=>0,'message'=>'更新失败');
	}
}

//根据身份证获取年龄
function get_age($idno=''){
	if(!is_card_no($idno)){
		return false; 
	}     
	$btime = strtotime(substr($idno, 6, 8));//idno是身份证号 截取日期并转为时间戳
	return get_date_distance($btime);
}

//根据开始时间，计算年份差
function get_date_distance($btime){
	$byear =date('Y',$btime );
	$bmonth =date('m',$btime );
	$bday =date('d',$btime );
	$curYear=date('Y');
	$curMoth = date('m');
	$curDay = date('d');
	$age = $curYear - $byear;
	if( $curMoth < $bmonth || ($curMoth ==$bmonth && $curDay < $bday)){
		$age--;
	}
	return $age;  
}

//验证省份证格式是否正确
function is_card_no($id){
	$id = strtoupper($id);
	$regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
	$arr_split = array();
	if(!preg_match($regx, $id)){
		return false;
	}
	if(15==strlen($id)){//检查15位
		$regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
		@preg_match($regx, $id, $arr_split);
		//检查生日日期是否正确
		$dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
		if(!strtotime($dtm_birth)) {
			return false;
		}else{
			return true;
		}
	}else{//检查18位
		$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
		@preg_match($regx, $id, $arr_split);
		$dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
		if(!strtotime($dtm_birth)){//检查生日日期是否正确
			return false;
		}else{
			//检验18位身份证的校验码是否正确。
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
			$arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
			$arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
			$sign = 0;
			for ( $i = 0; $i < 17; $i++ ) {
				$b = (int) $id[$i];
				$w = $arr_int[$i];
				$sign += $b * $w;
			}
			$n = $sign % 11;
			$val_num = $arr_ch[$n];
			if ($val_num != substr($id,17, 1)) {
				return false;
			}else{
				return true;
			}
		}
	}
}

function cut_str($str,$length){
	if(mb_strlen($str)>$length){
		return mb_substr($str,0,$length).'...';
	}else{
		return $str;
	}
}